package com.weitu.majorHazard.informationRegistration.controller;

import com.alibaba.fastjson.JSONArray;
import com.weitu.accessory.bean.AccessoryPackageInfo;
import com.weitu.accessory.service.AccessoryUtilService;
import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.majorHazard.informationRegistration.entity.ContractRecord;
import com.weitu.majorHazard.informationRegistration.entity.InformationRegistration;
import com.weitu.majorHazard.informationRegistration.service.InformationRegistrationService;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by smp on 2017/4/24.
 */
@Controller
@RequestMapping(value = "majorHazard/informationRegistration")
public class InformationRegistrationController extends BaseController{
    private static final String MODULE_BATH_PATH=BASE_JSP_PATH+"majorHazard/informationRegistration";

    private final InformationRegistrationService informationRegistrationService;

    private AccessoryUtilService accessoryUtilService;//附件

    @Autowired
    public InformationRegistrationController(InformationRegistrationService informationRegistrationService,AccessoryUtilService accessoryUtilService){
        this.informationRegistrationService=informationRegistrationService;
        this.accessoryUtilService=accessoryUtilService;
    }

    /**
     * 跳往登记信息页面
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(){
        return MODULE_BATH_PATH+"/list";
    }

    /**
     * 查询登记信息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Object listAjax(HttpServletRequest request){
        Map<String,Object> paramMap=new HashMap<String,Object>();
        putParametersIntoMap(paramMap);
        int count=this.informationRegistrationService.queryCount(paramMap);
        DataGridPagination dataGridPagination=DataGridPagination.getInstance(request,count);
        PageView pageView=dataGridPagination.getPageView();

        paramMap.put("pageView",pageView);
        List<InformationRegistration> informationRegistrationList=this.informationRegistrationService.queryList(paramMap);
        return dataGridPagination.flushDataGridData(informationRegistrationList);
    }

    /**
     * 跳到添加页面
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public ModelAndView add(){
        ModelAndView modelAndView=new ModelAndView();
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("addUserId",BaseController.getSessionUser().getId());
        map.put("addUserName",BaseController.getSessionUser().getRealName());
        map.put("addDeptId",BaseController.getSessionUser().getDepartment().getId());
        map.put("addDeptName",BaseController.getSessionUser().getDepartment().getName());
        map.put("addTime", DateTimeUtil.formatterDateTime());
        modelAndView.addObject("userMessage",map);
        modelAndView.setViewName(MODULE_BATH_PATH+"/add");
        return modelAndView;
    }

    /**
     * 添加基本信息
     * @param informationRegistration
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @SystemLog(module = "信息登记",methods = "重大危险源信息登记")
    public AjaxResult addAjax(InformationRegistration informationRegistration){
       informationRegistration.setId(IDGenerator.getId());
       informationRegistration.setDelState(0);
        boolean flag=this.informationRegistrationService.insertMajorHazardMessage(informationRegistration);
        if(flag){
            return AjaxResult.success("信息登记成功");
        }else {
            return AjaxResult.fail("信息登记失败");
        }
    }

    /**
     * 调到修改页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public ModelAndView edit(String id){
        InformationRegistration informationRegistration=this.informationRegistrationService.queryInformationRegistrationById(id);
        List<AccessoryPackageInfo> accessoryList =  accessoryUtilService.queryAccessoryByBusinessId(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("informationRegistration",informationRegistration);
        modelAndView.addObject("accessoryList",accessoryList);
        modelAndView.addObject("uploadUserId",BaseController.getSessionUser().getId());
        modelAndView.setViewName(MODULE_BATH_PATH+"/edit");
        return modelAndView;
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public ModelAndView detail(String id){
        InformationRegistration informationRegistration=this.informationRegistrationService.queryInformationRegistrationById(id);
        List<AccessoryPackageInfo> accessoryList =  accessoryUtilService.queryAccessoryByBusinessId(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("informationRegistration",informationRegistration);
        modelAndView.addObject("accessoryList",accessoryList);
        modelAndView.addObject("uploadUserId",BaseController.getSessionUser().getId());
        modelAndView.setViewName(MODULE_BATH_PATH+"/detail");
        return modelAndView;
    }
    /**
     * 编辑
     * @param informationRegistration
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @SystemLog(module = "信息登记",methods = "编辑重大危险源信息")
    public AjaxResult editAjax(InformationRegistration informationRegistration,String contractRecord){
        informationRegistration.setUpdateTime(DateTimeUtil.formatterDateTime());
        informationRegistration.setUpdateUserId(BaseController.getSessionUser().getId());
        informationRegistration.setUpdateDeptId(BaseController.getSessionUser().getDepartment().getId());
        List<ContractRecord> contractRecordList= JSONArray.parseArray(contractRecord,ContractRecord.class);
        for(int i=0;i<contractRecordList.size();i++){
            contractRecordList.get(i).setMessageId(informationRegistration.getId());
        }
        boolean flag=this.informationRegistrationService.updateMajorHazardMessage(informationRegistration,contractRecordList);
        if(flag){
            return AjaxResult.success("编辑成功");
        }else {
            return AjaxResult.fail("编辑失败");
        }
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @SystemLog(module = "信息登记",methods = "删除重大危险源信息")
    public AjaxResult delete(String id){
        int count=this.informationRegistrationService.deleteInformationRegistrationById(id);
        if(count>0){
            return AjaxResult.success("删除成功");
        }else {
            return AjaxResult.fail("删除失败");
        }
    }

    /**
     * 弹框页面
     * @return
     */
    @RequestMapping(value = "/contractRecord",method = RequestMethod.GET)
    public ModelAndView contractRecord(String beginDate,String endDate,String contractName){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("beginDate",beginDate);
        modelAndView.addObject("endDate",endDate);
        modelAndView.addObject("contractName",contractName);
        modelAndView.setViewName(MODULE_BATH_PATH+"/contractRecord");
        return modelAndView;
    }

    /**
     * 承包记录
     * @param messageId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/dataList",method = RequestMethod.POST)
    public List<ContractRecord> dataList(String messageId){
        List<ContractRecord> contractRecordList=this.informationRegistrationService.queryContractRecord(messageId);
        return contractRecordList;
    }

    /**
     * 简单list
     * @return
     */
    @RequestMapping(value = "/simpleList",method = RequestMethod.GET)
    public String simpleList(){
        return MODULE_BATH_PATH+"/simpleList";
    }
}
